package com.ruoyi.rms.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rms.domain.*;
import com.ruoyi.rms.enums.OrderStatus;
import com.ruoyi.rms.enums.RechargeStatus;
import com.ruoyi.rms.enums.SeatStatus;
import com.ruoyi.rms.service.*;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.OrderMessageMapper;

/**
 * 订单信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-08
 */
@Service
@Slf4j
public class OrderMessageServiceImpl implements IOrderMessageService 
{
    @Autowired
    private OrderMessageMapper orderMessageMapper;

    @Autowired
    private ISeatMessageService seatMessageService;

    @Autowired
    private IDishesOrderService dishesOrderService;

    @Autowired
    private IMemberMessageService memberMessageService;

    @Autowired
    private IMemberRechargeService memberRechargeService;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public OrderMessage selectOrderMessageByOrderId(Long orderId)
    {
        return orderMessageMapper.selectOrderMessageByOrderId(orderId);
    }

    /**
     * 查询某人的下单情况
     * @param wxId
     * @return
     */
    @Override
    public List<OrderMessage> selectOrderByWxId(String wxId) {
        // 只查询已付款状态
        return orderMessageMapper.selectOrderByWxId(wxId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param orderMessage 订单信息
     * @return 订单信息
     */
    @Override
    public List<OrderMessage> selectOrderMessageList(OrderMessage orderMessage)
    {
        return orderMessageMapper.selectOrderMessageList(orderMessage);
    }

    /**
     * 新增订单信息
     * 
     * @param orderMessage 订单信息
     * @return 结果
     */
    @Override
    public int insertOrderMessage(OrderMessage orderMessage)
    {
        orderMessage.setOrderId(IdUtils.createId());
        orderMessage.setCreateBy(SecurityUtils.getUsername());
        orderMessage.setCreateTime(DateUtils.getNowDate());
        orderMessage.setStatus(OrderStatus.UN_ORDER.getStatus()); // 初次创建设置为未下单状态
        return orderMessageMapper.insertOrderMessage(orderMessage);
    }

    /**
     * 新增订单信息 微信用户
     *
     * @param orderMessage 订单信息
     * @return 结果
     */
    @Override
    public int insertOrderMessageByWxId(OrderMessage orderMessage) {
        orderMessage.setOrderId(IdUtils.createId());
        orderMessage.setStatus(OrderStatus.UN_ORDER.getStatus()); // 初次创建设置为未下单状态
        orderMessage.setCreateTime(DateUtils.getNowDate());

        // 同时修改餐桌状态
        SeatMessage seatMessage = new SeatMessage();
        seatMessage.setSeatId(orderMessage.getSeatId());
        seatMessage.setStatus(SeatStatus.BEING_USED.getStatus()); // 改为正在使用状态
        seatMessageService.updateSeatMessageByWx(seatMessage);

        return orderMessageMapper.insertOrderMessage(orderMessage);
    }

    /**
     * 修改订单信息
     * 
     * @param orderMessage 订单信息
     * @return 结果
     */
    @Override
    public int updateOrderMessage(OrderMessage orderMessage)
    {
        // orderMessage.setUpdateBy(SecurityUtils.getUsername());
        orderMessage.setUpdateTime(DateUtils.getNowDate());
        return orderMessageMapper.updateOrderMessage(orderMessage);
    }

    /**
     * 修改订单信息 微信用户
     *
     * @param orderMessage 订单信息
     * @return 结果
     */
    @Override
    public int updateOrderMessageByWxId(OrderMessage orderMessage) {
        orderMessage.setUpdateTime(DateUtils.getNowDate());
        return orderMessageMapper.updateOrderMessage(orderMessage);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderMessageByOrderIds(Long[] orderIds)
    {
        return orderMessageMapper.deleteOrderMessageByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderMessageByOrderId(Long orderId)
    {
        return orderMessageMapper.deleteOrderMessageByOrderId(orderId);
    }

    /**
     * 结账
     * @param orderId
     * @return
     */
    @Override
    public int checkOut(Long orderId, Integer score) {
        OrderMessage orderMessage = orderMessageMapper.selectOrderMessageByOrderId(orderId);
        String status = orderMessage.getStatus();
        if (!OrderStatus.ORDERED.getStatus().equals(status)) {
            log.error("要结账的订单必须是已下单状态，结账失败");
            return RetUtils.ERROR;
        }

        // 相关餐桌变为空闲状态
        Long seatId = orderMessage.getSeatId();
        SeatMessage seatMessage = new SeatMessage();
        seatMessage.setSeatId(seatId);
        seatMessage.setStatus(SeatStatus.FREE.getStatus()); // 设为空闲
        seatMessageService.updateSeatMessageByWx(seatMessage);

        // 计算价格
        BigDecimal duePrize = BigDecimal.valueOf(0);
        List<DishesMessage> dishesMessageList = dishesOrderService.selectDishesByOrderId(orderId);
        for (DishesMessage dishesMessage : dishesMessageList) {
            int orderNum = dishesMessage.getOrderNum();
            duePrize = duePrize.add(dishesMessage.getPrice().multiply(BigDecimal.valueOf(orderNum)));
        }
        orderMessage.setDuePrize(duePrize);

        // 查看会员信息
        String wxId = orderMessage.getWxId();
        MemberMessage memberMessage = memberMessageService.getInfoByWxId(wxId);
        if (memberMessage == null || memberMessage.getPrice().compareTo(duePrize.multiply(memberMessage.getDiscount())) < 0) {
            // 不是会员或者余额不够，正常付款
            orderMessage.setActualPrize(duePrize);
        } else {
            // 是会员且余额充足，按折扣价付款
            orderMessage.setActualPrize(duePrize.multiply(memberMessage.getDiscount()));

            memberMessage.setPrice(memberMessage.getPrice().subtract(duePrize.multiply(memberMessage.getDiscount()))); // 修改余额
            memberMessageService.updateMemberMessage(memberMessage); // 更新数据

            MemberRecharge memberRecharge = new MemberRecharge();
            memberRecharge.setPrice(duePrize.multiply(memberMessage.getDiscount())); // 设置消费金额
            memberRecharge.setStatus(RechargeStatus.CONSUME.getStatus()); // 类型为消费
            memberRecharge.setWxId(wxId); // 设置消费者微信号
            if (memberMessage.getPhone() != null) memberRecharge.setPhone(memberMessage.getPhone()); // 设置手机号
            memberRechargeService.insertMemberRecharge(memberRecharge); // 添加消费记录
        }

        // 订单状态改为已付款状态
        orderMessage.setStatus(OrderStatus.PAYED.getStatus());
        if (score != null) orderMessage.setScore(score); // 设置评分
        return updateOrderMessage(orderMessage);
    }
}
